package message.visitor.singleton;

import message.types.message.RejectedRequest;
import message.types.stalker.CreateRoomSuccessAnswer;
import message.types.stalker.RegisterRequest;
import message.types.stalkerstring.GetRoomFromNameAnswer;
import message.types.string.CreatePrivatePersistentRoomRequest;
import message.types.string.CreateRoomRequest;
import message.types.string.GetRoomFromNameRequest;
import message.visitor.MessageVisitor;
import stalker.Stalker;
import stalker.room.PrivatePersistentRoom;
import stalker.room.PublicNoHistoryRoom;
import stalker.room.Room;

public class RoomManagerVisitor extends MessageVisitor<Void> {

  public RoomManagerVisitor() {
  }

  @Override
  public Void visit(CreateRoomRequest createRoomReq) {
    assert (createRoomReq != null);
    
    if ((!createRoomReq.getName().equals("")) && Room.isTaken(createRoomReq.getName())) {
      Stalker.ROOM_MANAGER_STALKER.acceptMessage(getRejectedMessage(createRoomReq));
    } else {
      String room_name = createRoomReq.getName();
      Room room = null;
      if (room_name.equals("")) {
        room = PublicNoHistoryRoom.create();
      } else {
        room = PublicNoHistoryRoom.create(room_name);
      }
      /*Stalker.ROOM_MANAGER_STALKER.acceptMessage(
          new CreateRoomSuccessAnswer(
              Stalker.ROOM_MANAGER_STALKER,
              createRoomReq.getClient(),
              room));*/
      room.acceptMessage(new RegisterRequest(Stalker.ROOM_MANAGER_STALKER, 
          room, createRoomReq.getClient()));
    }
    return null;
  }
  
  @Override
  public Void visit(CreatePrivatePersistentRoomRequest createRoomReq) {
    assert (createRoomReq != null);
    
    if ((!createRoomReq.getName().equals("")) && Room.isTaken(createRoomReq.getName())) {
      Stalker.ROOM_MANAGER_STALKER.acceptMessage(getRejectedMessage(createRoomReq));
    } else {
      String room_name = createRoomReq.getName();
      Room room = null;
      if (room_name.equals("")) {
        room = PrivatePersistentRoom.create();
      } else {
        room = PrivatePersistentRoom.create(room_name);
      }
      /*Stalker.ROOM_MANAGER_STALKER.acceptMessage(
          new CreateRoomSuccessAnswer(
              Stalker.ROOM_MANAGER_STALKER,
              createRoomReq.getClient(),
              room));*/
      room.acceptMessage(new RegisterRequest(Stalker.ROOM_MANAGER_STALKER, 
          room, createRoomReq.getClient()));
    }
    return null;
  }

  @Override
  public Void visit(CreateRoomSuccessAnswer createRoomReq) {
    assert (createRoomReq != null);
    createRoomReq.getDestination().acceptMessage(createRoomReq);
    return null;
  }
  
  @Override
  public Void visit(GetRoomFromNameRequest get_stalker_from_name) {
    assert (get_stalker_from_name != null);
    if (Room.isTaken(get_stalker_from_name.getName())) {
      //ok
      Stalker.ROOM_MANAGER_STALKER.acceptMessage(
          new GetRoomFromNameAnswer(Stalker.ROOM_MANAGER_STALKER,
                                    get_stalker_from_name.getClient(),
                                    Room.getRoomFromName(get_stalker_from_name.getName()),
                                    get_stalker_from_name.getName()));
    } else {
      // no match
      Stalker.ROOM_MANAGER_STALKER.acceptMessage(getRejectedMessage(get_stalker_from_name));
    }
    return null;
  }
  
  @Override
  public Void visit(GetRoomFromNameAnswer get_stalker_from_name) {
    assert (get_stalker_from_name != null);
    relayMessage(get_stalker_from_name);
    return null;
  }

  @Override
  public Void visit(RejectedRequest rejectedRequest) {
    
    assert rejectedRequest != null;
    
    if (rejectedRequest.getDestination().equals(Stalker.ROOM_MANAGER_STALKER)) {
      // message arrived
      assert false;
    } else {
      relayMessage(rejectedRequest);
    }
    
    return null;
  }
  
}
